package com.itheima.pinda.web.base;

import com.itheima.pinda.DTO.base.GoodsTypeDto;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.service.PdGoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author llhuang10
 * @date 2021/9/29 15:49
 */
@Slf4j
@Validated
@Api(tags = "货物类型管理")
@RestController
@RequestMapping("/base/goodsType")
public class GoodsTypeController {

    @Resource
    private PdGoodsTypeService goodsTypeService;

    /**
     * 新增货物类型, 同时要关联中间表车辆类型
     */
    @ApiOperation("新增货物类型")
    @PostMapping
    public GoodsTypeDto saveGoodsType(@Valid @RequestBody GoodsTypeDto dto) {
        return goodsTypeService.saveGoodsType(dto);
    }

    /**
     * 查询所有货物类型
     */
    @ApiOperation("查询所有货物类型")
    @GetMapping("/all")
    public List<GoodsTypeDto> findGoodsAll() {
        return goodsTypeService.findGoodsAll();
    }

    @ApiOperation("根据id查询货物类型")
    @GetMapping("/{id}")
    public GoodsTypeDto findById(@NotBlank(message = "货物类型Id不能为空!") @PathVariable String id) {
        // todo
        return goodsTypeService.findById(id);
    }

    /**
     * 获取分页货物类型数据
     * . @RequestParam(name = "page") 默认值就是非空的, 必填项
     * 如何不加上这个注解呢 ?
     *
     * @param page          页码
     * @param pageSize      每页
     * @param name          货物类型名称
     * @param truckTypeId   车辆类型Id
     * @param truckTypeName 车辆类型名称
     * @return
     */
    @ApiOperation("分页查询货物类型")
    @GetMapping("/page")
    public PageResponse<GoodsTypeDto> findGoodAllPage(
            @RequestParam(name = "page") Integer page,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "truckTypeId", required = false) String truckTypeId,
            @RequestParam(name = "truckTypeName", required = false) String truckTypeName) {

        return goodsTypeService.findByPage(page, pageSize, name, truckTypeId, truckTypeName);
    }

    @GetMapping("")
    @ApiOperation(value = "获取货物类型列表")
    public List<GoodsTypeDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids) {
        return goodsTypeService.findGoodsAll(ids);
    }

    /**
     * 更新货物类型信息
     *
     * @param id  货物类型Id
     * @param dto 货物类型信息
     * @return 货物类型信息
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新货物类型信息")
    public GoodsTypeDto update(@NotBlank(message = "货物类型Id不能为空!") @PathVariable(name = "id") String id,
                               @Valid @RequestBody GoodsTypeDto dto) {
        return goodsTypeService.updateGoods(id, dto);
    }

    @ApiOperation("删除货物类型")
    @DeleteMapping("{id}")
    public Result deleteGoodsById(@NotBlank(message = "货物类型Id不能为空!") @PathVariable String id) {
        goodsTypeService.deleteGoodsById(id);
        return Result.ok();
    }

}
